﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.Common;
using QuanLyKho.DataAccess;
using QuanLyKho.GUI.Library;

namespace QuanLyKho.GUI
{
    public partial class frmListWarehouse : Form
    {
        private static long ID = 0;
        public frmListWarehouse()
        {
            InitializeComponent();
            ;
        }
        public frmListWarehouse(long _ID)
        {
            InitializeComponent();
            txtCode.Focus();
            ID = _ID;
            binDataOneWarehouse(_ID);
        }


        private void binDataOneWarehouse(long _ID)
        {
            List<clsWarehouse> listWarehouse = new clsDataAccessWarehouse().ListWarehouseSelectOne(_ID);
            if (listWarehouse.Count > 0)
            {
                txtCode.Text = listWarehouse[0].Code;
                txtName.Text = listWarehouse[0].Name;
                txtDes.Text = listWarehouse[0].Des;
                txtAddress.Text = listWarehouse[0].Address;
                txtTel.Text = listWarehouse[0].Tel;
                txtManager.Text = listWarehouse[0].Manager;
                //cboWar.SelectedValue = listWarehouse[0].IDlistward;
            }
        }
        public void refresh()
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtDes.Text = "";
            txtAddress.Text = "";
            txtTel.Text = "";
        }

        private void butAddNew_Click(object sender, EventArgs e)
        {
            refresh();
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            string Code = txtCode.Text;
            string Name = txtName.Text;
            string Des = txtDes.Text;
            string Address = txtAddress.Text;
            string Tel = txtTel.Text;
            string Manager = txtManager.Text;
            long IDlistWard = Convert.ToInt64(cboWar.SelectedValue);
            DateTime Created = DateTime.Now;
            if (Code.Length < 1)
            {
                new clsLibrary().runLoading("Chưa điền mã kho!", 0);
                txtCode.Focus();
                return;
            }
            else
            {
                if (Code.Length > 255)
                {
                    new clsLibrary().runLoading("Mã kho không lớn hơn 255 ký tự.", 0);
                    txtCode.Focus();
                    return;
                }
            }
            if (Name.Length < 1)
            {
                new clsLibrary().runLoading("Chưa điền tên kho!", 0);
                txtName.Focus();
                return;
            }
            else
            {
                if (Name.Length > 255)
                {
                    new clsLibrary().runLoading("Tên kho không lớn hơn 255 ký tự.", 0);
                    txtName.Focus();
                    return;
                }
            }
            if (Tel.Length < 1)
            {
                new clsLibrary().runLoading("Chưa điền số điện thoại!", 0);
                txtName.Focus();
                return;
            }
            else
            {
                if (Tel.Length > 255)
                {
                    new clsLibrary().runLoading("Số điện thoại không lớn hơn 255 ký tự.", 0);
                    txtName.Focus();
                    return;
                }
            }
            clsWarehouse obj = new clsWarehouse();
            obj.Code = Code;
            obj.Name = Name;
            obj.Des = Des;
            obj.Address = Address;
            obj.Tel = Tel;
            obj.Manager = Manager;
            obj.IDlistward = IDlistWard;
            if (ID < 1)
            {
                if (new clsDataAccessWarehouse().AddNew(obj) > 0)
                {
                    new clsLibrary().runLoading("Đã thêm mới thông tin!", 2);
                    refresh();
                }
                else
                    new clsLibrary().runLoading("Có lỗi xảy ra khi thêm mới thông tin!", 0);
            }
            else
            {
                obj.ID = ID;
                if (new clsDataAccessWarehouse().Update(obj) > 0)
                {
                    new clsLibrary().runLoading("Thông tin đã được sửa!", 2);
                    refresh();

                    //this.Close();
                    ID = 0;
                }
                else
                    new clsLibrary().runLoading("Có lỗi xảy ra khi sửa thông tin!", 0);
            }

        }
        public void FillDatacombo()
        {
            try
            {
                List<clsWard> list = new clsDataAccessListWard().ListWardSelectAll();
                DataTable dt = new DataTable("clsWard");
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("Name", typeof(string));
                DataRow dr;
                foreach (clsWard obj in list)
                {
                    dr = dt.NewRow();
                    dr[0] = obj.ID;
                    dr[1] = obj.Name;
                    dt.Rows.Add(dr);
                }
                cboWar.DataSource = dt;
                cboWar.DisplayMember = dt.Columns["Name"].ToString();
                cboWar.ValueMember = dt.Columns["ID"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void frmListWarehouse_Load(object sender, EventArgs e)
        {
            FillDatacombo();
        }

        private void butExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtTel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
